Page({
  data: {
    username: '',
    password: '',
    error: ''
  },
  // 假数据用户
  users: [
    { username: 'admin', password: '123456', role: 'admin' },
    { username: 'teacher1', password: '123456', role: 'teacher' },
    { username: 'parent1', password: '123456', role: 'parent' }
  ],
  onUsernameInput(e) {
    this.setData({ username: e.detail.value });
  },
  onPasswordInput(e) {
    this.setData({ password: e.detail.value });
  },
  onLogin() {
    const { username, password } = this.data;
    const user = this.users.find(u => u.username === username && u.password === password);
    if (user) {
      // 存储用户名到全局和本地
      getApp().globalData = getApp().globalData || {};
      getApp().globalData.username = user.username;
      wx.setStorageSync('username', user.username);
      // 跳转到不同主页
      let url = '';
      if (user.role === 'admin') url = '/pages/admin/dashboard/dashboard';
      else if (user.role === 'teacher') url = '/pages/teacher/dashboard/dashboard';
      else if (user.role === 'parent') url = '/pages/parent/dashboard/dashboard';
      wx.redirectTo({ url });
    } else {
      this.setData({ error: '账号或密码错误' });
    }
  }
}); 